package org.jeecg.modules.sggg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.sggg.entity.SgCategory;
import org.jeecg.modules.sggg.entity.SgGoods;
import org.jeecg.modules.sggg.entity.SgMember;
import org.jeecg.modules.sggg.service.ISgCategoryService;
import org.jeecg.modules.sggg.service.ISgGoodsService;
import org.jeecg.modules.sggg.service.ISgMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: 商品类型表
 * @Author: jeecg-boot
 * @Date: 2023-03-30
 * @Version: V1.0
 */
@Api(tags = "商品类型表")
@RestController
@RequestMapping("/category/sgCategory")
@Slf4j
public class SgCategoryController extends JeecgController<SgCategory, ISgCategoryService> {
    @Autowired
    private ISgCategoryService sgCategoryService;

    @Autowired
    private ISgGoodsService sgGoodsService;

    @Autowired
    private ISgMemberService sgMemberService;

    /**
     * 分页列表查询
     *
     * @param sgCategory
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "商品类型表-分页列表查询")
    @ApiOperation(value = "商品类型表-分页列表查询", notes = "商品类型表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<SgCategory>> queryPageList(SgCategory sgCategory,
                                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                   HttpServletRequest req) {
        QueryWrapper<SgCategory> queryWrapper = QueryGenerator.initQueryWrapper(sgCategory, req.getParameterMap());
        Page<SgCategory> page = new Page<SgCategory>(pageNo, pageSize);
        queryWrapper.eq("sys_org_code", sgCategory.getSysOrgCode());
        queryWrapper.orderByAsc("sort");
        IPage<SgCategory> pageList = sgCategoryService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param sgCategory
     * @return
     */
    @AutoLog(value = "商品类型表-添加")
    @ApiOperation(value = "商品类型表-添加", notes = "商品类型表-添加")
    //@RequiresPermissions("category:sg_category:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SgCategory sgCategory) {
        sgCategoryService.save(sgCategory);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param sgCategory
     * @return
     */
    @AutoLog(value = "商品类型表-编辑")
    @ApiOperation(value = "商品类型表-编辑", notes = "商品类型表-编辑")
    //@RequiresPermissions("category:sg_category:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SgCategory sgCategory) {
        sgCategoryService.updateById(sgCategory);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "商品类型表-通过id删除")
    @ApiOperation(value = "商品类型表-通过id删除", notes = "商品类型表-通过id删除")
    //@RequiresPermissions("category:sg_category:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        sgCategoryService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "商品类型表-批量删除")
    @ApiOperation(value = "商品类型表-批量删除", notes = "商品类型表-批量删除")
    //@RequiresPermissions("category:sg_category:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.sgCategoryService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "商品类型表-通过id查询")
    @ApiOperation(value = "商品类型表-通过id查询", notes = "商品类型表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SgCategory> queryById(@RequestParam(name = "id", required = true) String id) {
        SgCategory sgCategory = sgCategoryService.getById(id);
        if (sgCategory == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(sgCategory);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param sgCategory
     */
    //@RequiresPermissions("category:sg_category:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SgCategory sgCategory) {
        return super.exportXls(request, sgCategory, SgCategory.class, "商品类型表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("category:sg_category:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SgCategory.class);
    }

    /**
     * TODO     商品类型上下架
     *
     * @param
     * @return
     * @author WangJie
     * @date 2023/3/31 9:10
     * @Exception
     */
    @AutoLog(value = "商品类型表-商品类型上下架")
    @ApiOperation(value = "商品类型表-商品类型上下架", notes = "商品类型表-商品类型上下架")
    @RequestMapping(value = "/isOnSale", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> isOnSale(@RequestBody SgCategory sgCategory) {
        sgCategory.setIsOnSale(sgCategory.getIsOnSale() == 0 ? 1 : 0);
        sgCategoryService.updateById(sgCategory);
        return Result.OK(sgCategory.getIsOnSale() == 0 ? "下架成功!" : "上架成功!");
    }


    /**
     * TODO	查询所有上架状态的分类
     *
     * @param
     * @return
     * @author WangJie
     * @date 2023/4/11 15:41
     * @Exception
     */
    @ApiOperation(value = "商品类型表-查询所有上架状态的分类", notes = "商品类型表-查询所有上架状态的分类")
    @GetMapping(value = "/queryList")
    public Result<List<Map<String, Object>>> queryList(String memberId) {
        SgMember sgMember = sgMemberService.getById(memberId);
        List<SgCategory> category = sgCategoryService.list(new QueryWrapper<SgCategory>().eq("is_on_sale", 1).eq("is_delete", 0).eq("sys_org_code", sgMember.getSysOrgCode()).orderByAsc("sort"));
        List<Map<String, Object>> list = new ArrayList<>();
        category.forEach(item -> {
            Map<String, Object> data = new HashMap<>();
            data.put("category", item);
            data.put("goods", sgGoodsService.list(new QueryWrapper<SgGoods>().eq("category_id", item.getId()).eq("is_delete", 0).eq("sys_org_code", sgMember.getSysOrgCode()).orderByDesc("sales")));
            list.add(data);
        });
        return Result.OK(list);
    }

    /**
     * 根据机构编码获取商品类型
     *
     * @param orgCode
     * @return
     */
    @GetMapping("/getCategoryByOrgCode")
    public Result<List<SgCategory>> getCategoryByOrgCode(String orgCode) {
        List<SgCategory> sgCategories = sgCategoryService.list(new QueryWrapper<SgCategory>().eq("sys_org_code", orgCode));
        return Result.ok(sgCategories);
    }
}
